﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using PloobsEngine.Physics;
using PloobsEngine.SceneControl;
using PloobsEngine.Input;
using PloobsEngine.Physics.XJigLib;
using PloobsEngine.Material;
using PloobsEngine.Cameras;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Light;
using PloobsEngine;

namespace Etapa1.Screens
{
    /// <summary>
    /// Water Scrren
    /// </summary>
    public class WaterScreen : SceneScreen
    {
        private JigLibxWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;
        private ICamera cam;
        WaterShader waterShader;
        
        public WaterScreen(EngineStuff es)
        {
            this.engine = es;            
            physicWorld = new JigLibxWorld();            
            mundo = new BrutalForceWorld(physicWorld);        
        }

        public override void LoadContent(EngineStuff es)
        {
            base.LoadContent(es);

            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            ///Oceano
            {
                ///Objeto fisico
                GhostObject pi2 = new GhostObject(new Vector3(0, 0, 0), Matrix.Identity, Vector3.One);
                ///Criacao do Shader (Parametros Default)
                waterShader = new WaterShader("..\\Content\\Textures\\cubemap");
                waterShader.SetDefault();
                waterShader.Initialize();

                ///Criacao e adicao do Material e do Objeto
                IMaterial mat = new Material(waterShader);
                IObject obj = new SimpleObject(mat, pi2, null);
                mundo.AddObject(obj);
            }

            ///Camera primeira pessoa
            cam = new CameraFirstPerson(true);

            ///Luzes direcionais
            Color color = Color.White;
            DirectionalLight ld1 = new DirectionalLight(Vector3.Left, color);
            DirectionalLight ld2 = new DirectionalLight(Vector3.Right, color);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, color);
            DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, color);
            DirectionalLight ld5 = new DirectionalLight(Vector3.Down, color);
            float li = 2f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            mundo.AddLight(ld1);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            mundo.AddLight(ld4);
            mundo.AddLight(ld5);

            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);
            //dr.isDebug = true;
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;

            ///Adicao dos Post effects            
            ///Antialising
            dr.AddPostEffect(new AntiAliasingPostEffect());
            ///HDR
            dr.AddPostEffect(new HdrPostEffect());

            mundo.AddCamera(cam);
            this.World = mundo;

        }

        /// <summary>
        /// Override do Update da Screen
        /// Normalmente os Inputs sao capturados usando o Component Input, neste caso
        /// usou-se simplesmente o Input do XNA para alterar os parametros do oceano
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="otherScreenHasFocus"></param>
        /// <param name="coveredByOtherScreen"></param>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.D1))
            {
                waterShader.WaterAmount = 0;
                waterShader.BumpHeight = .1f;
                waterShader.Initialize();
                waterShader.Restart();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D2))
            {
                waterShader.WaterAmount = 1;

                waterShader.ShallowWaterColor = Color.DarkSeaGreen;
                waterShader.DeepWaterColor = Color.Brown;
                waterShader.ReflectionColor = Color.DarkGray;
                waterShader.BumpHeight = .1f;
                waterShader.Initialize();
                waterShader.Restart();

            }

            if (Keyboard.GetState().IsKeyDown(Keys.D3))
            {                
                //shader2.ShallowWaterColor = Color.Gold;
                //shader2.DeepWaterColor = Color.Blue;
                //shader2.ReflectionColor = Color.White;                

                waterShader.BumpSpeed = new Vector2(0, .5f);                
                waterShader.FresnelPower = 5;
                waterShader.WaterAmount = 2;                
                waterShader.HDRMultiplier = 25;
                waterShader.BumpHeight = .7f;
                waterShader.WaveAmplitude = .1f;
                waterShader.WaterAmount = .1f;
                waterShader.WaveFrequency= .3f; 

                waterShader.Initialize();
                waterShader.Restart();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D4))
            {
                waterShader.WaveFrequency = .5f;
                waterShader.WaveAmplitude = .3f;                
                waterShader.BumpHeight = .1f; 
                waterShader.Initialize();
                waterShader.Restart();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D5))
            {
                waterShader.WaveFrequency = .1f;
                waterShader.WaveAmplitude = .1f;
                waterShader.BumpHeight = .1f;
                waterShader.BumpHeight = .1f; 
                waterShader.Initialize();
                waterShader.Restart();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D6))
            {
                waterShader.WaveFrequency = .1f;
                waterShader.WaveAmplitude = .5f;
                waterShader.BumpHeight = .1f;
                waterShader.Initialize();
                waterShader.Restart();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D7))
            {
                waterShader.WaveFrequency = .1f;
                waterShader.WaveAmplitude = 2f;
                waterShader.BumpHeight = 1f;
                waterShader.Initialize();
                waterShader.Restart();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D8))
            {
                waterShader.WaveFrequency = 0;
                waterShader.WaveAmplitude = 0;
                waterShader.BumpHeight = 0;
                waterShader.Initialize();
                waterShader.Restart();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D9))
            {
                waterShader.WaveFrequency = 0;
                waterShader.WaveAmplitude = 0;
                waterShader.BumpHeight = 1;
                waterShader.Initialize();
                waterShader.Restart();
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D0))
            {
                waterShader.SetDefault();
                waterShader.Initialize();
                waterShader.Restart();
            }

            ///MUST BE CALLED !!!            
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }



    }
}
